package shipNuking;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.ServerSocket;

public class Server extends Player {
	public Ship[][] fieldShipsPlayerOne = new Ship[10][10];
	public Ship[][] fieldShipsPlayerTwo = new Ship[10][10];
	public int[][] fieldHitsPlayerOne = new int[10][10];
	public int[][] fieldHitsPlayerTwo = new int[10][10];
	private ServerSocket serverSock;
	private int defeatedShipsPlayOne = 0;
	private int defeatedShipsPlayTwo = 0;

	public Server() {
		for (int i = 0; i <= 9; i++) {
			for (int j = 0; j <= 9; j++) {
				this.fieldShipsPlayerOne[i][j] = null;
				this.fieldShipsPlayerTwo[i][j] = null;
			}
		}
		try {
			this.serverSock = new ServerSocket(6666);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public int getDefeatedShips(int Player){
		if(Player == 1){
			return this.defeatedShipsPlayOne;
		}else if(Player == 2){
			return this.defeatedShipsPlayTwo;
		}else{
			return 0;
		}
	}
	
	public void increaseDefeatedShips(int Player){
		if(Player == 1){
			this.defeatedShipsPlayOne++;
		}
		if(Player == 2){
			this.defeatedShipsPlayTwo++;
		}
	}
	public ServerSocket getSocket() {
		return this.serverSock;
	}

	public boolean isShootAble(int x, int y, int[][] z) {
		if (x > 9 || x < 0 || y > 9 || y < 0) {
			return false;
		} else {
			return (z[x][y] == 0);
		}
	}

	public void shipPlacementRoutineServer(Ship z, Ship[][] ships) {
		boolean placed = false;
		BufferedReader bufferShip = new BufferedReader(new InputStreamReader(
				System.in));

		String cordAll;
		String[] splittedCordAll = new String[4];
		int[] splittedCordAllInt = new int[4];

		while (!placed) {
			try {
				cordAll = bufferShip.readLine();
				splittedCordAll = cordAll.split(":");
				
				if (splittedCordAll.length == 4) {
					for (int i = 0; i < 4; i++) {
						try {
							splittedCordAllInt[i] = Integer
									.parseInt(splittedCordAll[i]);
						} catch (NumberFormatException e) {
							System.out.println("Eine/mehrere Koordinaten waren keine Zahl!");
							placed = false;
						}
					}
					if (this.isPlaceAble(splittedCordAllInt[0],
							splittedCordAllInt[1], splittedCordAllInt[2],
							splittedCordAllInt[3], z, ships)) {
						this.placeShip(splittedCordAllInt[0],
								splittedCordAllInt[1], splittedCordAllInt[2],
								splittedCordAllInt[3], z, ships);
						placed = true;

					} else {
						System.out
								.println("Das Schiff kann an dieser Stelle nicht platziert werden. Geben sie "
										+ "die Koordinaten erneut ein.");
					}
				} else {
					placed = false;
					System.out
							.println("Das Schiff kann an dieser Stelle nicht platziert werden. Geben sie "
									+ "die Koordinaten erneut ein.");
				}
			} catch (NumberFormatException e) {
				System.out.println("Sie muessen eine Zahl eingeben!");
				placed = false;
			} catch (IOException e) {
				e.printStackTrace();
			}
			
		}
	}

	public boolean shipPlacementRoutineClient(Ship x, String userCord,
			Ship[][] fieldShipsPlayerTwo) {

		String[] splittedCordClient;
		int[] cordInt = new int[4];
		int arrayCounter = 0;
		Ship ship = x;
		arrayCounter = 0;
		userCord = userCord.trim();
		splittedCordClient = userCord.split(":");

		if (splittedCordClient.length != 4) {
			return false;
		}
		for (String a : splittedCordClient) {
			if (a.trim().equals("")) {
				return false;
			}
			try {
				cordInt[arrayCounter] = Integer.parseInt(a);
			} catch (NumberFormatException e) {
				return false;
			}
			arrayCounter++;
		}

		if (isPlaceAble(cordInt[0], cordInt[1], cordInt[2], cordInt[3], ship,
				fieldShipsPlayerTwo)) {
			placeShip(cordInt[0], cordInt[1], cordInt[2], cordInt[3], ship,
					fieldShipsPlayerTwo);
			return true;
		} else {
			return false;
		}

	}
}
